import React, { Component } from 'react'
import { connect } from 'dva';

import { Form, Input } from 'antd';
import { SearchRow, SearchCol, SearchButton } from '@/styled/form';
import { DICT } from '../enum';
import { DropDownSelectObj } from '@/components/DropDownSelect/index';
import { UmiComponentProps, SearchComponentProps, BaseComponentProps } from '@/common/type';

interface P {

}

const FormItem = Form.Item;

type Props = P & UmiComponentProps & SearchComponentProps & BaseComponentProps;

@connect(({ device_alarm }) => ({
  dict: device_alarm.dict,
  cond: device_alarm.doneCond || {},
}))
class Search extends Component<Props, any> {

  state = {
    type: null,
  }

  componentDidMount() {
    const { dispatch } = this.props;

    dispatch({
      type: 'device_alarm/queryDict',
      payload: {
        keyList: DICT
      }
    }).then(() => {
      const { cond, form } = this.props;
      const { pageNum, pageSize, ...searchParams } = cond;

      form.setFieldsValue(searchParams);
    });
  }

  search = e => {
    e.preventDefault()
    const { form } = this.props;
    form.validateFields((err, values) => {
      if (!err) {
        const { dispatch } = this.props;
        dispatch({
          type: 'device_alarm/updateDoneCond',
          payload: {
            ...values,
            fire: this.state.type
          }
        }).then(() => {
          this.props.initCurrent()
        })
      }
    });
  };

  handleChange = (type, value) => {
    const { cond = {}, dispatch } = this.props;
    cond[type] = value;

    dispatch({
      type: 'device_alarm/updateDoneCond',
      payload: cond
    }).then(() => {
      this.props.initCurrent()
    })
  }

  render() {
    const { form, dict = {} } = this.props;
    const { getFieldDecorator } = form;
    const formItemLayout = {
      labelCol: { span: 9 },
      wrapperCol: { span: 15 },
    };
    return (
      <Form onSubmit={this.search}>
        <SearchRow style={{ padding: '0 20px' }}>
          <SearchCol key={8} span={3}>
            <FormItem
              label="设备名称"
              {...formItemLayout}
            >
              {getFieldDecorator('deviceName', {
                rules: [{ message: '输入设备名称' }],
              })(
                <Input style={{ width: '100%' }} placeholder="设备名称" />
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={0} span={3}>
            <FormItem
              label="安装地址"
              {...formItemLayout}
            >
              {getFieldDecorator('installLocationName', {
                rules: [{ message: '输入安装地址' }],
              })(
                <Input style={{ width: '100%' }} placeholder="安装位置" />
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={1} span={3}>
            <FormItem
              label="设备类型"
              {...formItemLayout}
            >
              {getFieldDecorator('kind')(
                DropDownSelectObj({ dict, type: 'device.kind', onChange: (v) => this.handleChange('kind', v) })
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={2} span={3}>
            <FormItem
              label="报警类型"
              {...formItemLayout}
            >
              {getFieldDecorator('disPoseStatus')(
                DropDownSelectObj({ dict, type: 'device.state.status', onChange: (v) => this.handleChange('disPoseStatus', v) })
              )}
            </FormItem>
          </SearchCol>

          <SearchCol key={9} span={3}>
            <FormItem
              label="通讯地址"
              {...formItemLayout}
            >
              {getFieldDecorator('networkAddr', {
                rules: [{ message: '输入通讯地址' }],
              })(
                <Input style={{ width: '100%' }} placeholder="通讯地址" />
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={7} span={3}>
            <FormItem
              label="处理结果"
              {...formItemLayout}
            >
              {getFieldDecorator('resource')(
                DropDownSelectObj({ dict, type: 'device.dispose.resource', onChange: (v) => this.handleChange('resource', v) })
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={3} span={6}>
            <SearchRow type="flex" justify="end">
              <SearchButton onClick={() => {
                this.setState({ options: [] })
                this.props.form.resetFields();
              }} icon="redo"> 重置</SearchButton>
              <SearchButton htmlType="submit" type="primary" icon="search">搜索</SearchButton>
            </SearchRow>
          </SearchCol>
        </SearchRow>
      </Form>
    );
  }
}

const SearchS = Form.create<Props>()(Search);
export default SearchS;
